import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tool_plugin/tool_plugin.dart';
import 'package:toolbox/page/browser/browser_logic.dart';

import 'home_state.dart';

class HomeLogic extends GetxController {
  final HomeState state = HomeState();

  void onPopInvoked(bool didPop) async {
    if (state.currentPage == state.browserPage) {
      if (await Get.find<BrowserLogic>().goBack()) {
        return;
      }
    }
    ToolPluginMethodManager.instance.callBackDesktop();
    // return;
    // if(Scaffold.of(state.context).isDrawerOpen){
    //   Scaffold.of(state.context).closeDrawer();
    // }else{
    //   var result = DateTime.now().difference(state.backTime);
    //   if(result.inSeconds >= 1){
    //     showToast('连续返回以退出应用');
    //     state.backTime = DateTime.now();
    //   }else{
    //     exit(0);
    //   }
    // }
  }

  void showDrawer() {
    Scaffold.of(state.context).openDrawer();
  }

  void onTapTranslate(){
    onChangePage(state.translatePage);
  }

  void onTapQr(){
    onChangePage(state.qrPage);
  }

  void onTapVideoEdit(){
    onChangePage(state.videoEditPage);
  }

  void onTapBrowserPage(){
    onChangePage(state.browserPage);
  }

  void onChangePage(Widget page){
    state.currentPage = page;
    update();
    Scaffold.of(state.context).closeDrawer();
  }

  void onTapBg(){
    FocusManager.instance.primaryFocus?.unfocus();
  }

  void onTapStarWeb() {
    if (Get.isRegistered<BrowserLogic>()) {
      Get.find<BrowserLogic>().starWeb();
    }
  }
}
